package com.yuntu.controller;

import com.alibaba.fastjson.JSON;
import com.yuntu.pojo.Dev_user;
import com.yuntu.service.Dev_userService;
import com.yuntu.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.Date;

@Controller
@RequestMapping("/dev_user")
public class dev_userController {
    @Autowired
    private Dev_userService devService;

    @RequestMapping("/login")
    public String login(@RequestParam String devCode, @RequestParam String devPassword, HttpSession session) throws Exception {
        Dev_user dev_user=devService.login(devCode,devPassword);
        if (dev_user!=null){
            session.setAttribute("dev_user",dev_user);
            return "redirect:/dev_user/list";
        }else{
            session.setAttribute("error","账号或者密码不正确");
            return "login";
        }
    }
    @RequestMapping("/list")
    public String show(){
        return "index";
    }
    @ResponseBody
    @RequestMapping(value = "/getList",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"})
    public Object getList(@RequestParam Integer pageIndex)throws Exception{
        if (pageIndex==null){
            pageIndex=1;
        }
        if (pageIndex<1){
            pageIndex=1;
        }
        PageUtil<Dev_user> pageUtil=new PageUtil<>();
        pageUtil.setPageindex(pageIndex);
        pageUtil.setPagesize(3);
        devService.getDev_userPageAll(pageUtil);
        return JSON.toJSONStringWithDateFormat(pageUtil,"yyyy-MM-dd hh:mm:ss");
    }
    @ResponseBody
    @RequestMapping(value = "/delDev",method = RequestMethod.GET)
    public Object delDev(@RequestParam String id) {
        if (devService.delDev(Integer.parseInt(id)) != 0) {
            return "true";
        } else {
            return "false";
        }
    }
    @RequestMapping("/add")
    public String addDev(){
        return "register";
    }

    @ResponseBody
    @RequestMapping(value = "/isRepeat",method = RequestMethod.POST)
    public Object isRepeat(@RequestParam String devCode){
       Dev_user dev_user=devService.isRepeat(devCode);
       if (dev_user!=null){
           return JSON.toJSONString("exist");
       }else {
           return JSON.toJSONString("noexist");
       }
    }
    @ResponseBody
    @RequestMapping(value = "/addDevSave",method = RequestMethod.POST)
    public Object addDevSave(Dev_user dev_user,HttpSession session){
        Dev_user dev_user1=(Dev_user) session.getAttribute("dev_user");
        if (dev_user!=null){
            dev_user.setCreatedBy(dev_user1.getId());
        }else{
            dev_user.setCreatedBy(0);
        }
        dev_user.setCreationDate(new Date());
        if (devService.addDev(dev_user)!=0){
            return JSON.toJSONString("true");
        }else{
            return JSON.toJSONString("false");
        }
    }
    @RequestMapping(value = "/updDev/{id}",method = RequestMethod.GET)
    public String updDevSelById(@PathVariable Integer id, Model model)throws Exception{
        Dev_user dev_user=devService.selDev(id);
        model.addAttribute(dev_user);
        if (dev_user!=null){
            return "modify";
        }else{
            return "redirect:/dev_user/list";
        }
    }
    @ResponseBody
    @RequestMapping(value = "/updDevSave",method = RequestMethod.POST)
    public Object updDevSave(Dev_user dev_user,HttpSession session){
        Dev_user dev_user1=(Dev_user) session.getAttribute("dev_user");
        if (dev_user!=null){
            dev_user.setModifyBy(dev_user1.getId());
        }else{
            dev_user.setModifyBy(0);
        }
        dev_user.setModifyDate(new Date());
        if (devService.updDev(dev_user)!=0){
            return JSON.toJSONString(dev_user.getId());
        }else{
            return JSON.toJSONString("null");
        }
    }
}
